/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package evaluationtestsuits;

import dao.IdentificationType;
import dao.Users;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Diego
 */
public class EvaluationTestSuits {

    public static void main(String[] args) {
        System.out.println("Tests over the cloud-version");
//        UserFunctions.CreateUser();  
//        testWriting();
//        testUpdating();
        testFindByID();
//        testFindAllUsers();
        
        
//        UserFunctions.testDeleteUser();
//        UserFunctions.testFindAllUsers();
    }
    
    
    public static void testWriting(){
        System.out.println("Writing test:  20 simulated users create each 10 new user records");       
        try {
            final int THREAD_COUNT = 20; //Number of simulated users.
            long startTime;
            long endTime;
            final CountDownLatch startBarierr = new CountDownLatch(THREAD_COUNT + 1);
            final CountDownLatch finishBarierr = new CountDownLatch(THREAD_COUNT);
            for (int i = 0; i < THREAD_COUNT; i++) {
                final int iterationIndex = i;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        startBarierr.countDown();
                        System.out.println("Thread " + iterationIndex + " started");
                        try {
                            startBarierr.await();
                            for(int j=0; j<1; j++){
                               UserFunctions.CreateUser();  
                            }                                                                           
                            finishBarierr.countDown(); //current thread finished, send mark
                        } catch (InterruptedException e) {
                            throw new AssertionError("Unexpected thread interrupting");
                        }
                    }
                }).start();
            }
            startBarierr.countDown();
            startBarierr.await(); //await start for all thread
            startTime = System.currentTimeMillis(); //and note time
            finishBarierr.await(); //wait each thread
            endTime = System.currentTimeMillis();   //note finish time
            System.out.println("Test Time(ms) : " + (endTime - startTime));
        } catch (InterruptedException ex) {
            Logger.getLogger(EvaluationTestSuits.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void testUpdating(){
        System.out.println("Updating test:  20 simulated users update each 100 user records");       
        try {
            final int THREAD_COUNT = 20; //Number of simulated users.
            long startTime;
            long endTime;
            final CountDownLatch startBarierr = new CountDownLatch(THREAD_COUNT + 1);
            final CountDownLatch finishBarierr = new CountDownLatch(THREAD_COUNT);
            for (int i = 0; i < THREAD_COUNT; i++) {
                final int iterationIndex = i;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        startBarierr.countDown();
                        System.out.println("Thread " + iterationIndex + " started");
                        try {
                            startBarierr.await();
                            for(int j=0; j<1; j++){
                               UserFunctions.testEditUser();
                                System.out.println(j);
                            }                                                                           
                            finishBarierr.countDown(); //current thread finished, send mark
                        } catch (InterruptedException e) {
                            throw new AssertionError("Unexpected thread interrupting");
                        }
                    }
                }).start();
            }
            startBarierr.countDown();
            startBarierr.await(); //await start for all thread
            startTime = System.currentTimeMillis(); //and note time
            finishBarierr.await(); //wait each thread
            endTime = System.currentTimeMillis();   //note finish time
            System.out.println("Test Time(ms) : " + (endTime - startTime));
        } catch (InterruptedException ex) {
            Logger.getLogger(EvaluationTestSuits.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void testFindByID(){
        System.out.println("Find by ID test:  20 simulated users search 1 user record using the ID field.");       
        try {
            final int THREAD_COUNT = 20; //Number of simulated users.
            long startTime;
            long endTime;
            final CountDownLatch startBarierr = new CountDownLatch(THREAD_COUNT + 1);
            final CountDownLatch finishBarierr = new CountDownLatch(THREAD_COUNT);
            for (int i = 0; i < THREAD_COUNT; i++) {
                final int iterationIndex = i;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        startBarierr.countDown();
                        System.out.println("Thread " + iterationIndex + " started");
                        try {
                            startBarierr.await();
                            UserFunctions.testFindById();
                            finishBarierr.countDown(); //current thread finished, send mark
                        } catch (InterruptedException e) {
                            throw new AssertionError("Unexpected thread interrupting");
                        }
                    }
                }).start();
            }
            startBarierr.countDown();
            startBarierr.await(); //await start for all thread
            startTime = System.currentTimeMillis(); //and note time
            finishBarierr.await(); //wait each thread
            endTime = System.currentTimeMillis();   //note finish time
            System.out.println("Test Time(ms) : " + (endTime - startTime));
        } catch (InterruptedException ex) {
            Logger.getLogger(EvaluationTestSuits.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public static void testFindAllUsers(){
        System.out.println("Find all users test: 20 simulated users get each all user records..");       
        try {
            final int THREAD_COUNT = 20; //Number of simulated users.
            long startTime;
            long endTime;
            final CountDownLatch startBarierr = new CountDownLatch(THREAD_COUNT + 1);
            final CountDownLatch finishBarierr = new CountDownLatch(THREAD_COUNT);
            for (int i = 0; i < THREAD_COUNT; i++) {
                final int iterationIndex = i;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        startBarierr.countDown();
                        System.out.println("Thread " + iterationIndex + " started");
                        try {
                            startBarierr.await();
                            UserFunctions.testFindAllUsers();
                            finishBarierr.countDown(); //current thread finished, send mark
                        } catch (InterruptedException e) {
                            throw new AssertionError("Unexpected thread interrupting");
                        }
                    }
                }).start();
            }
            startBarierr.countDown();
            startBarierr.await(); //await start for all thread
            startTime = System.currentTimeMillis(); //and note time
            finishBarierr.await(); //wait each thread
            endTime = System.currentTimeMillis();   //note finish time
            System.out.println("Test Time(ms) : " + (endTime - startTime));
        } catch (InterruptedException ex) {
            Logger.getLogger(EvaluationTestSuits.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    
    
}
